﻿using Engine;
using Engine.Graphics;
using Game;

namespace Mekiasm
{
    public class MekProgressWidget : CanvasWidget
    {
        public enum ProgessType
        {
            bar, bidirectional, down, flame, installing_left, installing_right, large_left, large_right, right, horizontal_rate, vertical_rate,
            small_left, small_right, tall_right, progress1, progress2, progress_red, progress_black, progress_sblue, progress_purple,metal_former
        }
        public const string Path = "Mekiasm/Gui/Progress/";
        public ImageWidget img1, img2;
        public Vector2 imgSize;
        public Vector2 ProgressStartCoord = Vector2.Zero;
        public Vector2 ProgressEndCoord = Vector2.Zero;
        public Vector2 BackGroundStartCoord = Vector2.Zero;
        public Vector2 BackGroundEndCoord = Vector2.Zero;
        public Texture2D textureProgress;
        public Texture2D textureBackGround;
        /// <summary>
        /// 进度条渲染方向
        /// </summary>
        public enum ProgressRenderDirection
        {
            LeftToRight,//从左到右
            RightToLeft,//从右到左
            TopToBottom,//从上到下
            BottomToTop//从下到上
        }
        /// <summary>
        /// 进度条方向
        /// </summary>
        public enum ProgressDirection
        {
            Horizontal,//水平
            Vertial,//垂直
            HorizontalFlip,//水平翻转
            VertialFlip//垂直翻转
        }
        /// <summary>
        /// 纹理坐标渲染方向
        /// </summary>
        public enum TextureRenderDirection
        {
            TopLeftToBottomRight,//从左上到右下
            TopRightToBottomLeft,//从右上到左下
            BottomLeftToTopRight,//从左下到右上
            BottomRightToTopLeft//从右下到左上
        }

        public new Vector2 Size
        {
            set
            {
                imgSize = value;
            }
            get { return imgSize; }
        }

        public TextureRenderDirection TextureRender;

        public ProgressRenderDirection ProgressRender;

        public ProgressDirection progressDirection;

        public ProgessType progressType;
        /// <summary>
        /// 默认上下布局，左上到右下
        /// </summary>
        /// <param name="type"></param>
        public MekProgressWidget(ProgessType type)
        {
            SetProgressType(type);
        }

        public void SetProgressType(ProgessType type)
        {
            progressType = type;
            textureProgress = GetTexture(type);
            textureBackGround = textureProgress;
            InitCoordData();
        }

        public MekProgressWidget(Texture2D texture)
        {
            textureProgress = texture;
            textureBackGround = texture;
            InitCoordData();
        }
        /// <summary>
        /// 初始化进度条
        /// </summary>
        /// <param name="texture">进度层</param>
        /// <param name="texture2">背景层</param>
        public MekProgressWidget(Texture2D progressTexture, Texture2D backgroundTexture)
        {
            textureProgress = progressTexture;
            textureBackGround = backgroundTexture;
            InitCoordData();
        }

        public void InitCoordData()
        {
            if (img1 == null)
            {
                img1 = new ImageWidget();
                Children.Add(img1);
            }
            if (img2 == null)
            {
                img2 = new ImageWidget();
                Children.Add(img2);
            }
            //背景层设置
            img1.Texture = textureBackGround;
            //进度层设置
            img2.Texture = textureProgress;
            switch (progressType)
            {
                case ProgessType.small_right:
                case ProgessType.large_right:
                case ProgessType.metal_former:
                case ProgessType.tall_right:
                case ProgessType.right:
                case ProgessType.progress1:
                    TextureRender = TextureRenderDirection.TopLeftToBottomRight;
                    progressDirection = ProgressDirection.Horizontal;
                    break;
                case ProgessType.installing_left:
                case ProgessType.down:
                    TextureRender = TextureRenderDirection.TopLeftToBottomRight;
                    progressDirection = ProgressDirection.Vertial;
                    break;
                case ProgessType.progress_black:
                case ProgessType.progress_purple:
                case ProgessType.progress_red:
                case ProgessType.progress_sblue:
                case ProgessType.vertical_rate:
                case ProgessType.progress2:
                case ProgessType.flame:
                    TextureRender = TextureRenderDirection.BottomLeftToTopRight;
                    progressDirection = ProgressDirection.VertialFlip;
                    break;
                case ProgessType.small_left:
                case ProgessType.large_left:
                    TextureRender = TextureRenderDirection.TopRightToBottomLeft;
                    progressDirection = ProgressDirection.Horizontal;
                    break;
                case ProgessType.installing_right:
                    TextureRender = TextureRenderDirection.TopRightToBottomLeft;
                    progressDirection = ProgressDirection.Vertial;
                    break;

            }
            switch (TextureRender)
            {
                case TextureRenderDirection.TopLeftToBottomRight:
                    switch (progressDirection)
                    {
                        case ProgressDirection.Horizontal:
                            BackGroundStartCoord = new Vector2(0f, 0f);
                            BackGroundEndCoord = new Vector2(1f, 0.5f);
                            ProgressStartCoord = new Vector2(0f, 0.5f);
                            ProgressEndCoord = new Vector2(1f, 1f);
                            break;
                        case ProgressDirection.Vertial:
                            BackGroundStartCoord = new Vector2(0f, 0f);
                            BackGroundEndCoord = new Vector2(0.5f, 1f);
                            ProgressStartCoord = new Vector2(0.5f, 0f);
                            ProgressEndCoord = new Vector2(1f, 1f);
                            break;
                    }
                    break;
                case TextureRenderDirection.BottomRightToTopLeft:
                    switch (progressDirection)
                    {
                        case ProgressDirection.HorizontalFlip:
                            BackGroundStartCoord = new Vector2(0.5f, 0f);
                            BackGroundEndCoord = new Vector2(1f, 1f);
                            ProgressStartCoord = new Vector2(0f, 0f);
                            ProgressEndCoord = new Vector2(1f, 0.5f);
                            break;
                        case ProgressDirection.VertialFlip:
                            BackGroundStartCoord = new Vector2(0f, 0.5f);
                            BackGroundEndCoord = new Vector2(1f, 1f);
                            ProgressStartCoord = new Vector2(0f, 0f);
                            ProgressEndCoord = new Vector2(0.5f, 1f);
                            break;
                    }
                    break;
                case TextureRenderDirection.BottomLeftToTopRight:
                    switch (progressDirection)
                    {
                        case ProgressDirection.Horizontal:
                            BackGroundStartCoord = new Vector2(0.5f, 0f);
                            BackGroundEndCoord = new Vector2(1f, 1f);
                            ProgressStartCoord = new Vector2(0f, 0f);
                            ProgressEndCoord = new Vector2(1f, 0.5f);
                            break;
                        case ProgressDirection.VertialFlip:
                            BackGroundStartCoord = new Vector2(0f, 0f);
                            BackGroundEndCoord = new Vector2(0.5f, 1f);
                            ProgressStartCoord = new Vector2(0.5f, 0f);
                            ProgressEndCoord = new Vector2(1f, 1f);
                            break;
                    }
                    break;
                case TextureRenderDirection.TopRightToBottomLeft:
                    switch (progressDirection)
                    {
                        case ProgressDirection.HorizontalFlip:
                            BackGroundStartCoord = new Vector2(0.5f, 0f);
                            BackGroundEndCoord = new Vector2(1f, 1f);
                            ProgressStartCoord = new Vector2(0f, 0f);
                            ProgressEndCoord = new Vector2(1f, 0.5f);
                            break;
                        case ProgressDirection.Vertial:
                            BackGroundStartCoord = new Vector2(0.5f, 0f);
                            BackGroundEndCoord = new Vector2(1f, 1f);
                            ProgressStartCoord = new Vector2(0f, 0f);
                            ProgressEndCoord = new Vector2(0.5f, 1f);
                            break;
                    }
                    break;
            }
            img1.setTexcoord(BackGroundStartCoord, BackGroundEndCoord);
            SetProgress(0f);
        }
        public void SetProgress(float progress)
        {
            float EndX = 0;
            float EndY = 0;
            float StartX = 0;
            float StartY = 0;
            img1.Size = imgSize;

            switch (TextureRender)
            {
                case TextureRenderDirection.TopLeftToBottomRight:
                    switch (progressDirection)
                    {
                        case ProgressDirection.Horizontal:
                            StartX = 0f;
                            StartY = 0.5f;
                            EndX = ProgressStartCoord.X + (ProgressEndCoord.X - ProgressStartCoord.X) * progress;
                            EndY = 1f;
                            img2.Size = new Vector2(imgSize.X * progress, imgSize.Y);
                            img2.HorizontalAlignment = WidgetAlignment.Near;
                            img2.VerticalAlignment = WidgetAlignment.Near;
                            break;
                        case ProgressDirection.Vertial:
                            StartX = 0.5f;
                            StartY = 0;
                            EndX = 1f;
                            EndY = ProgressStartCoord.Y + (ProgressEndCoord.Y - ProgressStartCoord.Y) * progress;
                            img2.Size = new Vector2(imgSize.X, imgSize.Y * progress);
                            img2.HorizontalAlignment = WidgetAlignment.Near;
                            img2.VerticalAlignment = WidgetAlignment.Near;
                            break;
                    }
                    break;
                case TextureRenderDirection.BottomRightToTopLeft:
                    switch (progressDirection)
                    {
                        case ProgressDirection.HorizontalFlip:
                            EndX = 1f - (ProgressEndCoord.X + (ProgressStartCoord.X - ProgressEndCoord.X) * progress);
                            EndY = ProgressEndCoord.Y;
                            img2.Size = new Vector2(imgSize.X * progress, imgSize.Y);
                            img2.HorizontalAlignment = WidgetAlignment.Far;
                            img2.VerticalAlignment = WidgetAlignment.Near;
                            break;
                        case ProgressDirection.VertialFlip:
                            EndY = 1f - (ProgressEndCoord.Y + (ProgressEndCoord.Y - ProgressStartCoord.Y) * progress);
                            EndX = ProgressEndCoord.X;
                            img2.Size = new Vector2(imgSize.X, imgSize.Y * progress);
                            img2.HorizontalAlignment = WidgetAlignment.Near;
                            img2.VerticalAlignment = WidgetAlignment.Far;
                            break;
                    }
                    break;
                case TextureRenderDirection.BottomLeftToTopRight:
                    switch (progressDirection)
                    {
                        case ProgressDirection.Horizontal:
                            StartX = 0f;
                            StartY = 0.5f;
                            EndX = ProgressStartCoord.X + (ProgressEndCoord.X - ProgressStartCoord.X) * progress;
                            EndY = 1f;
                            img2.Size = new Vector2(imgSize.X * progress, imgSize.Y);
                            img2.HorizontalAlignment = WidgetAlignment.Near;
                            img2.VerticalAlignment = WidgetAlignment.Near;
                            break;
                        case ProgressDirection.VertialFlip:
                            StartX = ProgressStartCoord.X;
                            StartY = 1f - (ProgressStartCoord.Y + (ProgressEndCoord.Y - ProgressStartCoord.Y) * progress);
                            EndY = 1f;
                            EndX = 1f;
                            img2.Size = new Vector2(imgSize.X, imgSize.Y * progress);
                            img2.HorizontalAlignment = WidgetAlignment.Near;
                            img2.VerticalAlignment = WidgetAlignment.Far;
                            break;
                    }
                    break;
                case TextureRenderDirection.TopRightToBottomLeft:
                    switch (progressDirection)
                    {
                        case ProgressDirection.HorizontalFlip:
                            EndX = 1f - (ProgressEndCoord.X + (ProgressStartCoord.X - ProgressEndCoord.X) * progress);
                            EndY = ProgressEndCoord.Y;
                            img2.Size = new Vector2(imgSize.X * progress, imgSize.Y);
                            img2.HorizontalAlignment = WidgetAlignment.Far;
                            img2.VerticalAlignment = WidgetAlignment.Near;
                            break;
                        case ProgressDirection.Vertial:
                            StartX = 0f;
                            StartY = 0f;
                            EndY = (ProgressStartCoord.Y + (ProgressEndCoord.Y - ProgressStartCoord.Y) * progress);
                            EndX = 0.5f;
                            img2.Size = new Vector2(imgSize.X, imgSize.Y * progress);
                            img2.HorizontalAlignment = WidgetAlignment.Near;
                            img2.VerticalAlignment = WidgetAlignment.Near;
                            break;
                    }
                    break;
            }
            img2.setTexcoord(new Vector2(StartX,StartY), new Vector2(EndX, EndY));
        }
        public Texture2D GetTexture(ProgessType progessType)
        {
            return ContentManager.Get<Texture2D>(Path + progessType.ToString());
        }


    }
}
